﻿using System.Collections.Generic;
using System.Linq;
using OptimizationToolbox;

namespace GearSynth
{
    internal class ConvergeIfFeasible : abstractConvergence
    {

        private readonly abstractOptMethod optMethod;

        public ConvergeIfFeasible(abstractOptMethod optMethod)
        {
            this.optMethod = optMethod;
        }
        #region Overrides of abstractConvergence
        /// <summary>
        /// Has the optimization algorithm converged? Each criteria that overrides this is OR'ed together
        ///             that means only one critieria needs to return true.
        /// </summary>
        /// <param name="iteration">The number of iterations.</param><param name="numFnEvals">The number of function evaluations.</param><param name="fBest">The best f.</param><param name="xBest">The best x.</param><param name="population">The population of candidates.</param><param name="gradF">The gradient of F.</param>
        /// <returns>
        /// true or false - has the process converged?
        /// </returns>
        public override bool converged(long iteration, long numFnEvals, double fBest, IList<double> x, IList<double[]> population, IList<double> gradF)
        {
            return optMethod.feasible(x.ToArray());
        }

        #endregion
    }
}
